package com.olap.starter.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author WBG
 * @date 2021/10/23 9:18
 * @describe
 */
public class FileUtils {

    /**
     * 创建目录
     *
     * @param dirPath
     * @return
     */
    public static final boolean mkDir(String dirPath) {
        File dir = new File(dirPath);
        if (!dir.exists()) {
            return dir.mkdirs();
        }
        return true;
    }

    /**
     * @param filePath
     * @return 获取新的文件
     * @throws Exception
     */
    public static File getNewFile(String filePath) throws Exception {
        return getNewFile(filePath, false);
    }

    public static File getNewFile(String filePath, boolean existsDelete) throws Exception {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        if (file.exists()) {
            if (existsDelete) {
                file.delete();
            }
        } else {
            file.createNewFile();
        }
        return file;
    }

    public static File mkdirs(String filePath) {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        return file;
    }

    public static void saveFileByByte(String path, byte[] data) throws Exception {
        FileOutputStream out = new FileOutputStream(mkdirs(path));
        out.write(data);
        out.close();
    }

    public static void delFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
    }

    /**
     * 将文本保存成文件
     *
     * @param filePath
     * @param fileData
     */
    public static void saveFileByStr(String filePath, String fileData) {
        try {
            File file = new File(filePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter writer = new FileWriter(file, true);
            writer.write(fileData);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readTextFile(File file) throws IOException {
        StringBuffer sb = new StringBuffer();
        BufferedReader reader = null;
        if (file.exists()) {
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    sb.append(tempStr);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            throw new IOException("文件不存在：" + file);
        }
        return sb.toString();
    }

    /**
     * 读取文本文件
     *
     * @param filePath 文件路径
     * @return
     */
    public static String readTextFile(String filePath) throws IOException {
        StringBuffer sb = new StringBuffer();
        File file = new File(filePath);
        BufferedReader reader = null;
        if (file.exists()) {
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    sb.append(tempStr);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            throw new IOException("文件不存在：" + filePath);
        }
        return sb.toString();
    }

    /**
     * 读取文本文件
     *
     * @param filePath 文件路径
     * @return
     */
    public static List<String> readTextListFile(String filePath) throws IOException {
        List<String> list = new ArrayList<>();
        File file = new File(filePath);
        BufferedReader reader = null;
        if (file.exists()) {
            try {
                reader = new BufferedReader(new FileReader(file));
                String tempStr;
                while ((tempStr = reader.readLine()) != null) {
                    list.add(tempStr);
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw e;
            } finally {
                if (null != reader) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } else {
            throw new IOException("文件不存在：" + filePath);
        }
        return list;
    }

    public static byte[] getFileBytes(String file) {
        try {
            File f = new File(file);
            int length = (int) f.length();
            byte[] data = new byte[length];
            new FileInputStream(f).read(data);
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        List<String> strings = readTextListFile("C:\\Users\\76127\\Desktop\\新建 文本文档.txt");
        StringBuffer sb = new StringBuffer();
        String join = String.join(",", strings);
        System.out.println(join);
//        String path = "E:\\project\\2023s\\2024\\online-serve\\src\\main\\java\\com\\olp\\online\\pojo";
//        File file = new File(path);
//        for (File listFile : file.listFiles()) {
//            List<String> strings = readTextListFile(listFile.getPath());
//            StringBuffer sb = new StringBuffer();
//            for (String string : strings) {
//                if(string.indexOf("io.swagger.v3") != -1 || string.indexOf("@Schema(name = \"") != -1){
//                    continue;
//                }
//                if(string.indexOf("@Schema(description") != -1){
//                    string = convertToJavadoc(string);
//                }
//                sb.append(string+"\n");
//            }
//            saveFileByStr("e:\\test\\"+listFile.getName(),sb.toString());
//        }
    }

    public static String convertToJavadoc(String input) {
        // 正则表达式，匹配@Schema(description = "...")，其中"..."是任意字符（非换行符）
        String regex = "@Schema\\(description\\s*=\\s*\"([^\"]*)\"\\)";
        // 替换为Javadoc风格的注释
        return input.replaceAll(regex, "/**\n" + "     * $1\n" + "     */");
    }
}
